/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis;

import com.cxhd.lis.model.SendJson;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import java.util.List;

/**
 *
 * @author chuoxi
 */
public class JsonCtrl extends Controller {

    ControllerBind controll;

    public JsonCtrl() {
       // controll = this.getClass().getAnnotation(ControllerBind.class);
        //System.out.println(controll.viewPath()+"---------------------");

    }
    private SendJson json = new SendJson();

    protected void sendJsonError(String msg) {
        renderJson("{\"code\":500,\"msg\":\" " + msg + " \"}");
    }

    protected void setJsonData(String key, Model m) {
        json.setData(key, m);
    }

    protected void setJsonData(Model m) {

        json.setData("data", m);
    }

    protected void setJsonData(List list) {

        json.setData("data", list);
    }

    protected void setJsonData(Object obj) {
        json.setData("data", obj);
    }

    protected void setJsonData(Page page) {
        if (page != null) {
            json.setData("list", page.getList());
            json.setData("total", page.getTotalRow());
        } else {
            json.code = 200;
        }
    }

    protected void setJsonData(String key, List m) {
        json.setData(key, m);
    }

    protected void setJsonData(String key, Object value) {
        json.setData(key, value);
    }

    protected void sendJson(Page page) {
        setJsonData(page);
        renderJson(json.toJson());
    }

    protected void sendJson(List list) {
        setJsonData("data", list);
        renderJson(json.toJson());
    }

    protected void sendJson(String key, Object obj) {

        renderJson(json.setData(key, obj).toJson());
    }

    protected void sendJson(String key, List m) {
        json.setData(key, m);
        renderJson(json.toJson());
    }

    protected void sendJson(int code, SendJson result) {
        if (code == 200) {
            renderJson(result.toJson());
        } else {
            sendJson(code);
        }
    }

    protected void sendJson(SendJson result) {
        renderJson(result.toJson());
    }

    protected void sendJson() {
        renderJson(json.toJson());
    }

    protected void sendJson(boolean result) {
        int code = 200;
        if (!result) {
            code = 200;
        }
        sendJson(code);
    }

    protected void setJsonCode(int code) {
        setJsonData("code", code);
    }

    protected void sendJson(int code, String msg) {
        setJsonData("code", code);
        setJsonData("msg", msg);
        renderJson(json.toJson());
    }

    protected void sendJson(int code) {
        setJsonCode(code);
        renderJson(json.toJson());
    }

    protected void sendJson(Model m) {
        setJsonData(m);

        renderJson(json.toJson());
    }
}
